Skip to content

Conversation

xrmx
Copy link
Contributor

@xrmx xrmx commented Sep 9, 2025

Description

This fixes the following example:

from opentelemetry._logs import get_logger_provider
from opentelemetry._logs import LogRecord as APILogRecord

logger = get_logger_provider().get_logger(__name__)

logger.emit(APILogRecord(body="Hello, World!", attributes={"foo": "bar"}))

In both console:

{
    "body": "Hello, World!",
    "severity_number": null,
    "severity_text": null,
    "attributes": {
        "foo": "bar"
    },
    "dropped_attributes": 0,
    "timestamp": null,
    "observed_timestamp": "2025-09-09T08:41:35.905784Z",
    "trace_id": "0x00000000000000000000000000000000",
    "span_id": "0x0000000000000000",
    "trace_flags": 0,
    "resource": {
        "attributes": {
            "telemetry.sdk.language": "python",
            "telemetry.sdk.name": "opentelemetry",
            "telemetry.sdk.version": "1.37.0.dev0",
            "service.name": "unknown_service"
        },
        "schema_url": ""
    },
    "event_name": ""
}

And otlp exporters:

2025-09-09T09:34:21.086+0200	info	ResourceLog #0
Resource SchemaURL: 
Resource attributes:
     -> telemetry.sdk.language: Str(python)
     -> telemetry.sdk.name: Str(opentelemetry)
     -> telemetry.sdk.version: Str(1.37.0.dev0)
     -> service.name: Str(unknown_service)
ScopeLogs #0
ScopeLogs SchemaURL: 
InstrumentationScope __main__ 
LogRecord #0
ObservedTimestamp: 2025-09-09 07:34:21.083054264 +0000 UTC
Timestamp: 1970-01-01 00:00:00 +0000 UTC
SeverityText: 
SeverityNumber: Unspecified(0)
Body: Str(Hello, World!)
Attributes:
     -> foo: Str(bar)
Trace ID: 
Span ID: 
Flags: 0
	{"otelcol.component.id": "debug", "otelcol.component.kind": "Exporter", "otelcol.signal": "logs"}

This is an alternative version of #4737 with one more fix and no api change.

Refs #4319

Type of change

Please delete options that are not relevant.

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

How Has This Been Tested?

Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration

  • tox

Does This PR Require a Contrib Repo Change?

  • Yes. - Link to PR:
  • No.

Checklist:

  • Followed the style guidelines of this project
  • Changelogs have been updated
  • Unit tests have been added
  • Documentation has been updated

@xrmx xrmx requested a review from a team as a code owner September 9, 2025 08:52
@xrmx xrmx force-pushed the fix-apilogrecord-serialization branch from e4dafa8 to 5c30e21 Compare September 9, 2025 09:04
@xrmx xrmx moved this to Ready for review in @xrmx's Python PR digest Sep 9, 2025
@xrmx xrmx mentioned this pull request Sep 9, 2025
13 tasks
Copy link
Member

@aabmass aabmass left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than Dylan's comments LGTM

…r.emit

So that the layers below would not care about any difference.

Fixes:

Exception while exporting Log.
Traceback (most recent call last):
  File "/home/rm/src/opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/_shared_internal/__init__.py", line 179, in _export
    self._exporter.export(
  File "/home/rm/src/opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py", line 98, in export
    self.out.write(self.formatter(data.log_record))
  File "/home/rm/src/opentelemetry-python/opentelemetry-sdk/src/opentelemetry/sdk/_logs/_internal/export/__init__.py", line 90, in <lambda>
    formatter: Callable[[LogRecord], str] = lambda record: record.to_json()
AttributeError: 'LogRecord' object has no attribute 'to_json'
@xrmx xrmx force-pushed the fix-apilogrecord-serialization branch from 9fac00d to d98d472 Compare September 10, 2025 07:42
@xrmx xrmx enabled auto-merge (squash) September 10, 2025 15:59
@xrmx xrmx merged commit 22d1fd1 into open-telemetry:main Sep 10, 2025
467 of 469 checks passed
@github-project-automation github-project-automation bot moved this from Ready for review to Done in @xrmx's Python PR digest Sep 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

6 participants